package com.video.manage.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.video.manage.base.Result;
import com.video.manage.entity.InformationEntity;
import com.video.manage.mapper.InformationDao;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

@Slf4j
@RestController
public class InformationController {

    @Autowired
    private InformationDao informationDao;

    /**
     * 资讯列表
     * @param informationEntity
     * @return
     */
    @GetMapping("/information/list")
    public Result<InformationEntity> list(@ModelAttribute InformationEntity informationEntity){
        Result result = new Result();
        result.setCode(200);
        try {
            QueryWrapper<InformationEntity> wrapper = new QueryWrapper<>();
            wrapper.setEntity(informationEntity);
            wrapper.orderByDesc("create_time");
            List<InformationEntity> list = informationDao.selectList(wrapper);
            result.setList(list);
        }catch (Exception e){
            log.error("InformationController-list-error:",e);
            result.setCode(500);
            result.setMessage("查询失败");
        }
        return result;
    }

    /**
     * 保存资讯
     * @param dto
     * @return
     */
    @PostMapping("/information/add")
    public Result<InformationEntity> click(@RequestBody InformationEntity dto) {
        Result result = new Result();
        result.setCode(200);
        try {
            InformationEntity informationEntity = new InformationEntity();
            informationEntity.setTitle(dto.getTitle());
            informationEntity.setContent(dto.getContent());
            informationEntity.setUrl(dto.getUrl());
            informationEntity.setCreateTime(new Date());
            informationDao.insert(informationEntity);
            result.setEntity(informationEntity);
        } catch (Exception e) {
            log.error("资讯保存成功：", e);
            result.setCode(500);
            result.setMessage("资讯保存失败");
        }
        return result;
    }
}
